<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12"><span class="upload-text">商品图片上传</span></el-col>
      <el-col :span="12"><span class="upload-text">图片效果预览</span></el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-upload
                class="upload-demo"
                drag
                :limit="limit"
                :action="uploadUrl"
                :on-success="uploadSucc"
                :on-remove="handleRemove"
                :on-exceed = "onExceed">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-col>
      <el-col :span="12">
        <el-image style="width: 300px; height: 200px" :src="form.pic" fit="contain"></el-image>
      </el-col>
    </el-row>
    <el-row :gutter="20" style=" margin-top: 50px">
    <el-col :span="12">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" clearable></el-input>
        </el-form-item>
        <el-form-item label="详情" prop="detail">
          <el-input v-model="form.detail" clearable></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="number" :min="0" v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">完成</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  
  </div>
</template>

<script>
  export default {
    name: "GoodAdd",
    data() {
      let priceRule = (rule, value, callback)=>{
        if(value < 0){
          callback(new Error("价格不能小于0！"))
        }else{
          callback()
        }
      };

      return {
        uploadUrl: this.$store.state.takeaway + '/upload',
        limit: 1,

        form:{
          id: "",
          pic:"",
          name: "",
          detail: '',
          price: 0,
        },
        rules:{
          name:[
            {required:true,message: '请输入商品名称',trigger:'blur'},
            {min:1,max:16,message:'长度在 1 到 16 个字符',trigger:'blur'},
          ],
          detail:[
            {max:50,message:'最多 50 个字符',trigger:'blur'},
          ],
          price:[
            {required:true,message: '价格不能为空',trigger:'blur'},
            {validator: priceRule,trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      uploadSucc(res){
        console.log("上穿图片名称："+res);
        this.form.pic = this.$store.state.getImage + res;
      },
      handleRemove(file) {
        // console.log(file);
        this.$axios.post("/delFile",{
          path:file.response
        }).then(res=>{
          console.log(res);
          this.form.pic = ""
        }).catch(err=>{
          console.log(err)
        })
      },
      onExceed(files, fileList){
        this.$message.error("提示：只能上传一张图片！")
      },
      onSubmit(form){
        // console.log(this.form)
        this.$refs[form].validate((value)=>{
          if(value){
            const data = this.form;
            data['userId'] = this.$store.state.userId;
            this.$axios.post('/goodAdd',data)
            .then(res=>{
              if(res.data.code == 200){
                this.$message.success(res.data.message)
              }else{
                this.$message.error(res.data.message)
              }
            }).catch(err=>{
              console.log(err)
            })
          }else{
            console.log('error submit!!');
            return false;
          }
        })
      }
    },
    created() {
      const row = this.$route.params.row;
      console.log("新增/编辑：" + row);
      if(row && typeof(row)!='undefined' && row!=0){
        this.form.pic = row.pic;
        this.form.id = row.id;
        this.form.name  = row.name;
        this.form.detail = row.detail;
        this.form.price = row.price;
      }
    }
  }
</script>

<style scoped>
  .upload-text{
    font-size: 24px;
    color: #929292;
  }
</style>